<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="矿机机器ID" prop="machineId">-->
<!--        <el-input-->
<!--          v-model="queryParams.machineId"-->
<!--          placeholder="请输入矿机机器ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="矿机名称" prop="machineName">
        <el-input
          v-model="queryParams.machineName"
          placeholder="请输入矿机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="购买用户ID" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入购买用户ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="购入花费金额" prop="buyTotalAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.buyTotalAmount"-->
<!--          placeholder="请输入购入花费金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="购买算力数量" prop="buyQty">-->
<!--        <el-input-->
<!--          v-model="queryParams.buyQty"-->
<!--          placeholder="请输入购买算力数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="矿机总产矿天数" prop="totalMineDay">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalMineDay"-->
<!--          placeholder="请输入矿机总产矿天数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="目前已挖多少天" prop="mineDay">-->
<!--        <el-input-->
<!--          v-model="queryParams.mineDay"-->
<!--          placeholder="请输入目前已挖多少天"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="算力 fil/T" prop="power">-->
<!--        <el-input-->
<!--          v-model="queryParams.power"-->
<!--          placeholder="请输入算力 fil/T"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="开始挖矿时间" prop="beginTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.beginTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择开始挖矿时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="结束时间" prop="endTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.endTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择结束时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否返佣 0否 1是" prop="isReferee">-->
<!--        <el-input-->
<!--          v-model="queryParams.isReferee"-->
<!--          placeholder="请输入是否返佣 0否 1是"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['api:mUserMachine:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['api:mUserMachine:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['api:mUserMachine:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['api:mUserMachine:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="mUserMachineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
<!--      <el-table-column label="矿机机器ID" align="center" prop="machineId" />-->
      <el-table-column label="矿机名称" align="center" prop="machineName" />
<!--      <el-table-column label="矿机类型 1 免费矿机  2收益率矿机 3固定收益矿机" align="center" prop="machineType" >-->
      <el-table-column label="矿机类型" align="center" prop="machineType" >
        <template scope="scope">
          <span v-if="scope.row.machineType=== 1 " >免费矿机</span>
          <span v-else-if="scope.row.machineType === 2" >收益率矿机</span>
          <span v-else-if="scope.row.machineType === 3" >固定收益矿机</span>
        </template>
      </el-table-column>
      <el-table-column label="购买用户ID" align="center" prop="userId" />
      <el-table-column label="购入花费金额" align="center" prop="buyTotalAmount" />
      <el-table-column label="购买算力数量" align="center" prop="buyQty" />
      <el-table-column label="矿机总产矿天数" align="center" prop="totalMineDay" />
      <el-table-column label="目前已挖多少天" align="center" prop="mineDay" />
      <el-table-column label="算力 fil/T" align="center" prop="power" />
      <el-table-column label="开始挖矿时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态  0 未启动 1挖矿中 2已完成" align="center" prop="status" >-->
      <el-table-column label="状态" align="center" prop="status" >
        <template scope="scope">
          <span v-if="scope.row.status=== 0 " >未启动</span>
          <span v-else-if="scope.row.status === 1" >挖矿中</span>
          <span v-else-if="scope.row.status === 2" >已完成</span>
        </template>
      </el-table-column>
      <el-table-column label="是否返佣" align="center" prop="isReferee">
        <template scope="scope">
          <span v-if="scope.row.isReferee=== 0 " >否</span>
          <span v-else-if="scope.row.isReferee === 1" >是</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['api:mUserMachine:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['api:mUserMachine:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户矿机信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="矿机机器ID" prop="machineId">
          <el-input v-model="form.machineId" placeholder="请输入矿机机器ID" />
        </el-form-item>
        <el-form-item label="矿机名称" prop="machineName">
          <el-input v-model="form.machineName" placeholder="请输入矿机名称" />
        </el-form-item>
        <el-form-item label="购买用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入购买用户ID" />
        </el-form-item>
        <el-form-item label="购入花费金额" prop="buyTotalAmount">
          <el-input v-model="form.buyTotalAmount" placeholder="请输入购入花费金额" />
        </el-form-item>
        <el-form-item label="购买算力数量" prop="buyQty">
          <el-input v-model="form.buyQty" placeholder="请输入购买算力数量" />
        </el-form-item>
        <el-form-item label="矿机总产矿天数" prop="totalMineDay">
          <el-input v-model="form.totalMineDay" placeholder="请输入矿机总产矿天数" />
        </el-form-item>
        <el-form-item label="目前已挖多少天" prop="mineDay">
          <el-input v-model="form.mineDay" placeholder="请输入目前已挖多少天" />
        </el-form-item>
        <el-form-item label="算力 fil/T" prop="power">
          <el-input v-model="form.power" placeholder="请输入算力 fil/T" />
        </el-form-item>
        <el-form-item label="开始挖矿时间" prop="beginTime">
          <el-date-picker clearable
            v-model="form.beginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始挖矿时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否返佣 0否 1是" prop="isReferee">
<!--          <el-input v-model="form.isReferee" placeholder="请输入是否返佣 0否 1是" />-->
          <el-radio-group v-model="form.isForce">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMUserMachine, getMUserMachine, delMUserMachine, addMUserMachine, updateMUserMachine } from "@/api/api/mUserMachine";

export default {
  name: "MUserMachine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户矿机信息表格数据
      mUserMachineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        machineId: null,
        machineName: null,
        machineType: null,
        userId: null,
        buyTotalAmount: null,
        buyQty: null,
        totalMineDay: null,
        mineDay: null,
        power: null,
        beginTime: null,
        endTime: null,
        status: null,
        isReferee: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户矿机信息列表 */
    getList() {
      this.loading = true;
      listMUserMachine(this.queryParams).then(response => {
        this.mUserMachineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        machineId: null,
        machineName: null,
        machineType: null,
        userId: null,
        buyTotalAmount: null,
        buyQty: null,
        totalMineDay: null,
        mineDay: null,
        power: null,
        beginTime: null,
        endTime: null,
        status: 0,
        createTime: null,
        isReferee: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户矿机信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMUserMachine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户矿机信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMUserMachine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMUserMachine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户矿机信息编号为"' + ids + '"的数据项？').then(function() {
        return delMUserMachine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/mUserMachine/export', {
        ...this.queryParams
      }, `mUserMachine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
